//
//  MateListViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/9/28.
//

#import "MateListViewController.h"
#import "MateModel.h"
#import "MateListTableViewCell.h"
@interface MateListViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation MateListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_hideBaseNavBar = YES;
    self.view.backgroundColor = UIColor.clearColor;
    // Do any additional setup after loading the view.
}
- (void)SQ_viewWillAppear:(BOOL)animated{
    if (self.jsonArray.count<=0) {
        [self SQ_Header];
    }
}
- (void)SQ_UIViewForSelfView{
//    UIImageView * bgImageView = [[UIImageView alloc]init];
//    bgImageView.image = SQGetImage(@"二级背景");
//    [self.view addSubview:bgImageView];
//    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
//    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
    }];
    [self SQ_tableViewAddRefresh];
}
- (void)SQ_Header{
    [self.Dict setObject:@"1" forKey:@"current"];
    [self.Dict setObject:@"20" forKey:@"size"];
    [self.Dict setObject:self.type forKey:@"type"];
    if ([UserModel share].lat > 0) {
        [self.Dict setObject:@([UserModel share].lat) forKey:@"lat"];
        [self.Dict setObject:@([UserModel share].lng) forKey:@"lng"];
    }
    [HttpTool post:@"lug/list" paramDic:self.Dict successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page = 1;
        [self.tableView.mj_header endRefreshing];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [MateModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)SQ_Foother{
  
    [self.Dict setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [self.Dict setObject:@"20" forKey:@"size"];
    [self.Dict setObject:self.type forKey:@"type"];
    if ([UserModel share].lat > 0) {
        [self.Dict setObject:@([UserModel share].lat) forKey:@"lat"];
        [self.Dict setObject:@([UserModel share].lng) forKey:@"lng"];
    }
    [HttpTool post:@"lug/list" paramDic:self.Dict successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [MateModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return self.jsonArray.count ;
}

 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MateListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MateListTableViewCell"];
    if (!cell) {
        cell = [[MateListTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"MateListTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.jsonArray  objectAtIndex:indexPath.item];
    WS(weakSelf);
    [cell setTouchBlock:^(MateModel * _Nonnull model) {
        [weakSelf.jsonArray removeObject:model];
        [weakSelf.tableView reloadData];
    }];
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [MateListTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cellContentViewWidth:0 bottomOffset:10];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}
-(void)SQ_SetMate:(NSMutableDictionary*)dic{
    [self.Dict removeAllObjects];
    self.Dict  = [[NSMutableDictionary alloc]initWithDictionary:dic];
    [self SQ_Header];
}
 
- (NSMutableDictionary *)Dict{
    if (!_Dict) {
        _Dict = [[NSMutableDictionary alloc]init];
    }
    return _Dict;
}

@end
